***************************************************
***************************************************
**********                               ********** 
**********    Voters born after 1959	 ********** 
**********                               **********    
***************************************************
***************************************************

global beg_path `" XXX "'   // Indicate your working directory

use `"${beg_path}Data\Macro\Base_dataset"', clear
gen country_to_merge = country


merge m:1 country_to_merge year using `"${beg_path}Data\Macro\UN_gen.dta"' 
drop _merge 
drop if country ==.  


*Getting rid of individuals that have not reached the voting age 
foreach var of varlist UN_gen_a_* {
local x = substr("`var'", -2, 2)
replace `var' = . if `x' < Voting_age
}
 
*Generating total voting age population
egen total_VAP = rowtotal(UN_gen_a_* UN_gen_G_80and_more) 
 
*Generating year of birth
foreach var of varlist UN_gen_a* {
local x = substr("`var'", -2, 2)
gen yob_`var' = year - `x'
}
gen yob_UN_gen_G_80and_more = year - 80

* Setting as missing all those cohorts that were born before 1960
foreach var of varlist UN_gen_a* {
replace `var' = . if yob_`var' <  1960
} 
replace UN_gen_G_80and_more = . if yob_UN_gen_G_80and_more < 1960

*Summing all post-1959 cohorts 
egen after_1959_total = rowtotal(UN_gen_a_* UN_gen_G_80and_more) 

*Share of post-1959 cohorts  
gen after_1959_share = after_1959_total/total_VAP

*Correcting for missing info 
replace after_1959_share =0 if year < 1950 /* The UN population data start only in 1950 but we can code as 0 all the pre-1950 years*/

collapse after_1959_share, by(country_to_merge year)
rename country_to_merge country
lab var after_1959_share "Post-1959 Cohorts"
save `"${beg_path}Data\Macro\share_after_1959.dta"', replace
